<?php

namespace app\storeapi\controller\apps\card;

use app\storeapi\controller\Controller;
use app\storeapi\model\card\Card as CardModel;
/**
 * 储值卡管理
 * Class Goods
 * @package app\store\controller\apps\sharp
 */
class Card extends Controller
{


    /**
     * 获取储值卡
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function cardList()
    {
        $param = $this->postData();
        // 获取全部列表
        $model = new CardModel;
        $list = $model->cardList();
        return $this->renderSuccess($list);
    }

    /**
     * 储值卡详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        $model = CardModel::detail($param['card_id'])?:[];
        return $this->renderSuccess($model);
    }

    /**
     * 编辑储值卡
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        $model = CardModel::detail($param['card_id']);
        if(empty($model)){
            return $this->renderError('储值卡不存在，请刷新后重试');
        }
        if ($model->edit($param)) {
            return $this->renderSuccess([],"编辑成功");
        }
        return $this->renderError($model->getError() ?: '编辑失败');

    }


    /**
     * 添加储值卡
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $param = $this->postData();
        // 获取全部列表
        $model = new CardModel;
        if ($model->add($param)) {
            return $this->renderSuccess([],"添加成功");
        }
        return $this->renderError($model->getError() ?: '添加失败');

    }


    /**
     * 删除储值卡
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function del()
    {
        $param = $this->postData();
        $model = CardModel::detail($param['card_id']);
        if(empty($model)){
            return $this->renderError('储值卡不存在，请刷新后重试');
        }
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');

    }



}